# SPDX-FileCopyrightText: 2025 MounRiver Studio <https://mounriverstudio.com/>
# SPDX-License-Identifier: BSD-3-Clause
# Author: Azolla <https://gitee.com/azolla/>
# Date: 2025-04-21

# <INTERFACE|PUBLIC|PRIVATE>：指定编译定义的作用范围：
# INTERFACE：表示这些编译定义仅应用于依赖该目标的其他目标，而不应用于目标本身。
# PUBLIC：   表示这些编译定义既应用于目标本身，也应用于依赖该目标的其他目标。
# PRIVATE：  表示这些编译定义仅应用于目标本身，不影响依赖该目标的其他目标。

# Enable CMake support for ASM and C languages
project("driver" VERSION 1.0.0 LANGUAGES C ASM)

message(STATUS "Building driver...")

# 创建一个(接口库 INTERFACE)/(静态库 STATIC)/(动态库 SHARED)
add_library(driver INTERFACE)
 
# 忽略未使用的参数
target_compile_options(driver INTERFACE -Wno-unused-parameter) 

# 设置编译器选项
target_compile_definitions(driver INTERFACE
    CH32V208x_D8W=1
    $<$<CONFIG:Debug>:DEBUG>
)

target_sources(driver INTERFACE
    ${CMAKE_CURRENT_LIST_DIR}/Core/core_riscv.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_adc.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_dbgmcu.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_dma.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_exti.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_flash.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_gpio.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_i2c.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_iwdg.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_misc.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_opa.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_pwr.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_rcc.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_spi.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_tim.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_usart.c
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/src/ch32v20x_wwdg.c
    ${CMAKE_CURRENT_LIST_DIR}/System/system_ch32v20x.c
    ${CMAKE_CURRENT_LIST_DIR}/Debug/debug.c
)

target_include_directories(driver INTERFACE
    ${CMAKE_CURRENT_LIST_DIR}/Core
    ${CMAKE_CURRENT_LIST_DIR}/Peripheral/inc
    ${CMAKE_CURRENT_LIST_DIR}/System
    ${CMAKE_CURRENT_LIST_DIR}/Debug
)

if(CONFIG_FREERTOS)
    target_sources(driver INTERFACE ${CMAKE_CURRENT_LIST_DIR}/Startup/startup_ch32v20x_D8W_FreeRTOS.S)
    set(LINK_DIR ${CMAKE_CURRENT_LIST_DIR}/Ld/Link_FreeRTOS.ld CACHE PATH "Link directory")
else()
    target_sources(driver INTERFACE ${CMAKE_CURRENT_LIST_DIR}/Startup/startup_ch32v20x_D8W.S)
    set(LINK_DIR ${CMAKE_CURRENT_LIST_DIR}/Ld/Link.ld CACHE PATH "Link directory")
endif()

